package com.pinyoumei.app.activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.pinyoumei.app.R;
import com.pinyoumei.app.utils.StatusBarUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * 引导界面
 */
public class GuideActivity extends AppCompatActivity{
    private ViewPager viewPager_guide;
    private LinearLayout linearLayout_point;//圆形小图标的线性布局容器
    private int[] imageViewIdArray;//图片资源数组 R.drable.xxx 形式存放整形数据
    private List<ImageView> imageView_list;//图片资源的存放的List
    private RelativeLayout relative_button;//当滑动到最后一个图片的时候需要显示立即体验这个图片
    private ImageView imageView_point;//原点图片的ImageView
    private ImageView[] imageViewArray_point;//存放ImageView原点图片的ImageView数组
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_guide);
        StatusBarUtils.setTranslucentStatus(this);
        viewPager_guide = findViewById(R.id.viewPager_guide);
        //控件的初始化
        linearLayout_point = findViewById(R.id.linearLayout_point);
        initViewPager();

    }
    //初始化ViewPager
    private void initViewPager(){
        imageViewIdArray = new int[]{/*R.mipmap.guide_one,R.mipmap.guide_two,R.mipmap.guide_three,R.mipmap.guide_four*/};//初始化整形数组
        imageView_list = new ArrayList<>();//创建一个List的集合
        //创建一个LayoutParams参数 设置属性为全屏
        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        int length = imageViewIdArray.length;//获取存放资源图片的整形数据的长度
        for(int i=0;i<length;i++){
            //循环创建ImageView存放到集合中
            final ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);//设置展示的图片的属性
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            if(i==0){
                imageView.setImageResource(imageViewIdArray[i]);
            }else{
                /*Glide.with(this).load(imageViewIdArray[i]).into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        imageView.setImageDrawable(resource);
                    }
                });*/
                 if(i==3){
                     imageView.setOnClickListener(new View.OnClickListener() {
                         @Override
                         public void onClick(View v) {
//                             startActivity(new Intent(GuideActivity.this,LoginMainActivity.class));
//                             finish();
                         }
                     });
                 }
                Glide.with(this)
                        .load(imageViewIdArray[i])
                        .into(imageView);
            }
            imageView_list.add(imageView);//将加载好的图片的ImageView存放到List集合中
        }
        //设置ViewPager的Adapter
        viewPager_guide.setAdapter(new GuidePageAdapter(imageView_list));
        //设置ViewPager的滑动监听
        viewPager_guide.addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
            @Override
            public void onPageScrolled(int i, float v, int i1){

            }

            @Override
            public void onPageSelected(int position){
                //循环设置当前的标记图
//                int length = imageViewIdArray.length;
//                for(int i=0;i<length;i++){
//                    imageViewArray_point[position].setBackgroundResource(R.mipmap.icon_check_checked);
//                    if(position !=i){
//                        imageViewArray_point[i].setBackgroundResource(R.mipmap.icon_check_normal);
//                    }
//                }
//                //判断是否是最后一页，如果是则显示图片上的按钮
//                if(position == imageViewIdArray.length-1){//数组对应的下标是否跟当前图片的position
//                    relative_button.setVisibility(View.VISIBLE);
//                    linearLayout_point.setVisibility(View.GONE);
//                }else{
//                    relative_button.setVisibility(View.GONE);
//                    linearLayout_point.setVisibility(View.VISIBLE);
//                }
            }
            @Override
            public void onPageScrollStateChanged(int i){

            }
        });
    }
    //加载底部原点
//    private void initPoint(){
//        imageViewArray_point = new ImageView[imageView_list.size()];//创建一个ImageView数组存放原点图片
//        //循环创建底部原点ImageView,将生成的ImageView保存到数组中
//        int size = imageView_list.size();
//        for(int i=0;i<size;i++){
//            imageView_point = new ImageView(this);
//            imageView_point.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//            //imageView_point.setPadding(DensityUtils.dip2px(GuideActivity.this,9),0,DensityUtils.dip2px(GuideActivity.this,9),0);
//            imageViewArray_point[i]=imageView_point;//将创建好的ImageView放入对应下标的数组中
//            //第一个页面需要设置选中状态，这里采用2张不同的图片
//            if(i==0){
//                imageView_point.setBackgroundResource(R.mipmap.icon_check_checked);
//            }else{
//                imageView_point.setBackgroundResource(R.mipmap.icon_check_normal);
//            }
//            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(new ViewGroup.LayoutParams(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT));
//            layoutParams.leftMargin = DensityUtils.dip2px(GuideActivity.this,9);//设置点点点view的左边距
//            //layoutParams.rightMargin = DensityUtils.dip2px(GuideActivity.this,9);//设置点点点view的右边距
//            //将数组中的ImageView加入到ViewGroup中也就是那个Liner
//            linearLayout_point.addView(imageViewArray_point[i],layoutParams);
//        }
//    }
    //物理返回键的监听
    private long exitTime = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event){
        if(keyCode == KeyEvent.KEYCODE_BACK  && event.getAction()==KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis() - exitTime)>2000){
                Toast.makeText(getApplicationContext(),"再按一次退出程序",Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            }else{
                finish();
                //System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
     class GuidePageAdapter extends PagerAdapter {
        private List<ImageView>  viewList;
        public GuidePageAdapter(List<ImageView> viewList){//类的初始化
            this.viewList = viewList;
        }
        @Override
        public int getItemPosition(@NonNull Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public int getCount() {
            return viewList != null ? viewList.size():0;
        }

        @Override
        public boolean isViewFromObject(@NonNull View view, @NonNull Object o) {
            return view == o;
        }

        @NonNull
        @Override
        public Object instantiateItem(@NonNull ViewGroup container, int position) {
            container.addView(viewList.get(position));
            return viewList.get(position);
        }

        @Override
        public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
            container.removeView(viewList.get(position));
        }
    }

}
